﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Arizu.DAL;
using Arizu.Entity;

namespace Arizu.Repository
{
    public class AirizuCalendar : BaseProvider
    {
        #region
        private static volatile AirizuCalendar instance;
        private static object lockHelper = new object();

        private AirizuCalendar()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AirizuCalendar CreateInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new AirizuCalendar();
                    }
                }
            }
            return instance;
        }
        #endregion

        /// <summary>
        /// 插入评价信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public long Insert(CalendarEntity calendar)
        {
            string sql = "INSERT INTO Calendar(Price,State,RoomID, StartTime, EndTime) VALUES (@Price,@State,@RoomID, @StartTime, @EndTime)";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@Price", calendar.Price);
            SqlParameter parm_2 = new SqlParameter("@State", calendar.State);
            SqlParameter parm_3 = new SqlParameter("@RoomID", calendar.RoomID);
            SqlParameter parm_4 = new SqlParameter("@StartTime", calendar.StartTime);
            SqlParameter parm_5 = new SqlParameter("@EndTime", calendar.EndTime);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3, parm_4, parm_5 };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

                //calendar.ID = long.Parse(ds.Tables[0].Rows[0][0].ToString());

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            return calendar.ID;
        }

        public DataTable GetCalendars(long roomID)
        {
            DataTable dt = new DataTable();
            string sql = "select * from Calendar where RoomID=" + roomID;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public DataTable GetCalendars(long roomID, long startTime, long endTime)
        {
            DataTable dt = new DataTable();
            string sql = "select * from Calendar where RoomID=" + roomID + " and StartTime>" + startTime + " and EndTime<" + endTime;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public void UpdateCalendar(long roomID, int state, int price, long startTime, long endTime)
        {
            string sql = "Update Calendar Set Price=@Price, State=@State, StartTime=@StartTime, EndTime=@EndTime where RoomID=@RoomID";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@RoomID", roomID);
            SqlParameter parm_2 = new SqlParameter("@Price", price);
            SqlParameter parm_3 = new SqlParameter("@State", state);
            SqlParameter parm_4 = new SqlParameter("@StartTime", startTime);
            SqlParameter parm_5 = new SqlParameter("@EndTime", endTime);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3, parm_4, parm_5 };
            #endregion
            try
            {
                SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
